﻿/*====================================================================================
	TASK 11:                                                                   {Arrays}
    Write a program that finds the index of given element in a sorted array of integers 
    by using the binary search algorithm (find it in Wikipedia). 
=====================================================================================*/

using System;

class BinarySearchInStoredArray
{
    static void Main()
    {
//=========================================================================
//                SORTING ARRAY with "SELECTION SORT"                        
//=========================================================================
        //int[] arr = { 1, 3, 4, 5, 2 };

        //int min;
        //int minPosition = 0;
        //int exchanger;

        //for (int i = 0; i < arr.Length; i++)
        //{
        //    min = arr[i];
        //    minPosition = i;

        //    for (int j = i + 1; j < arr.Length; j++)
        //    {
        //        if (arr[j] < min)
        //        {
        //            min = arr[j];
        //            minPosition = j;
        //        }
        //    }

        //    exchanger = arr[i];
        //    arr[i] = min;
        //    arr[minPosition] = exchanger;

        //}

        //for (int i = 0; i < arr.Length; i++)
        //{
        //    Console.Write(arr[i] + " ");
        //}
        //Console.WriteLine("\n");
        //Console.WriteLine(new string('=', 40));
        //Console.WriteLine();


//=========================================================================
//                           "BINARY SEARCH"
//=========================================================================

        int[] binaryArr = { -50,-10,-5,-1,0,1, 3, 8, 9, 12, 18, 25, 30,32,62,102,110,250,333,331,336,339,560,1000,4546546,54654564 };
        int theNumber = 12;//Searching number
        int start = 0;
        int end = binaryArr.Length-1;
        int candidatePosition = candidatePosition = (end - start) / 2; ;
        int counter = 0;
        int log;
        bool searching = true;

        while (searching)
        {
            
            if (binaryArr[candidatePosition] == theNumber)
            {
                searching = false;
                Console.WriteLine("Searching number is here");
            }
            else if (binaryArr[candidatePosition] > theNumber)
            {
                end = candidatePosition;
                candidatePosition = (end - start) / 2;
            }
            else if (binaryArr[candidatePosition] < theNumber)
            {
                start = candidatePosition;
                candidatePosition =candidatePosition + (end - start) / 2;
            }

            counter++;
            log = 2 ^ counter;

            if ((log >= binaryArr.Length) && searching == true)
            {
                Console.WriteLine("Searching number is not here");
                break;
            }
        }
    }
}

